package com.douding.server.service;

import com.douding.server.domain.*;
import com.douding.server.dto.*;
import com.douding.server.mapper.CourseMapper;
import com.douding.server.mapper.my.MyCourseMapper;
import com.douding.server.util.CopyUtil;
import com.douding.server.util.UuidUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.ibatis.annotations.Param;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

import java.util.Date;

import static org.springframework.beans.BeanUtils.copyProperties;

@Service
public class CourseService {

    @Resource
    private CourseMapper courseMapper;

    @Resource
    private MyCourseMapper myCourseMapper;

    @Resource
    private CourseCategoryService courseCategoryService;

    private static final Logger LOG = LoggerFactory.getLogger(CourseService.class);

    /**
     * 列表查询：关联课程分类表 web接口
     *
     * @param pageDto
     */
    public void list(CoursePageDto pageDto) {
        PageHelper.startPage(pageDto.getPage(), pageDto.getSize());
        List<CourseDto> courseList = myCourseMapper.list(pageDto);
        PageInfo<CourseDto> pageInfo = new PageInfo<>(courseList);
        pageDto.setTotal(pageInfo.getTotal());
        List<CourseDto> courseDtoList = CopyUtil.copyList(courseList, CourseDto.class);
        pageDto.setList(courseDtoList);
    }

    /**
     * 查找某一课程，供web模块用，只能查已发布的
     *
     * @param id
     * @return
     */
    public CourseDto findCourse(String id) {
        CourseDto courseDto = new CourseDto();
        Course course = courseMapper.selectCourseById(id);
        BeanUtils.copyProperties(course, courseDto);
        return courseDto;
    }

    /**
     * 新课列表查询，只查询已发布的，按创建日期倒序，3门
     */
    public List<CourseDto> listNew() {
        List<Course> courseList = courseMapper.selectListNew();
        List<CourseDto> courseDtoList = CopyUtil.copyList(courseList, CourseDto.class);
        // SQL里面已经 按创建日期倒序 排好序了，取前面三条
        List<CourseDto> threeNewCourseList = new ArrayList<>();
        int count = 3;
        for (CourseDto c : courseDtoList) {
            if (count == 0) {
                break;
            }
            threeNewCourseList.add(c);
            count--;
        }
        return threeNewCourseList;
    }

    // 更新课程时长
    public void updateTime(@Param("courseId") String courseId) {
        myCourseMapper.updateTime(courseId);
    }

    // 课程内容相关的操作 查找 新增,修改
    public CourseContent findContent(String id) {
        return courseMapper.selectContentById(id);
    }

    // 更新内容
    public void saveContent(CourseContentDto courseContentDto) {
        CourseContent courseContent = new CourseContent();
        copyProperties(courseContentDto, courseContent);
        // 课程id一定存在，进行更新操作
        if (courseContent.getId() != null) {
            // 更新内容
            courseMapper.updateContent(courseContent);
        }
    }

    public void sort(SortDto sortDto) {
        int newSort = sortDto.getNewSort();
        // 更新排序字段
        courseMapper.updateCourseSort(newSort);
    }

    // 查询
    public void selectByPage(CoursePageDto pageDto) {
        PageHelper.startPage(pageDto.getPage(), pageDto.getSize());
        List<Course> courseList = courseMapper.selectList();
        PageInfo<Course> pageInfo = new PageInfo<>(courseList);
        pageDto.setTotal(pageInfo.getTotal());
        List<CourseDto> courseDtoList = CopyUtil.copyList(courseList, CourseDto.class);
        pageDto.setList(courseDtoList);
    }

    // 新增 或 更新 （加上事务注解，失败直接回滚，防止脏数据）
    @Transactional
    public void save(CourseDto courseDto) {
        Course course = new Course();
        copyProperties(courseDto, course);
        if (course.getId() != null) {
            course.setUpdatedAt(new Date());
            // 更新
            courseMapper.update(course);
        } else {
            String courseId = UuidUtil.getShortUuid();
            course.setId(courseId);
            course.setCreatedAt(new Date());
            course.setUpdatedAt(new Date());
            // 新增
            // 课程关联种类表信息入库
            courseCategoryService.saveBatch(courseId, courseDto.getCategorys());
            // 课程关联内容表信息入库，默认刚创建课程，内容为 "" 空字符串
            CourseContent courseContent = new CourseContent();
            courseContent.setId(courseId);
            courseContent.setContent("");
            courseMapper.insertContent(courseContent);
            // 课程表的信息入库
            courseMapper.insert(course);
        }
    }

    // 删除
    public void deleteById(String id) {
        courseMapper.deleteById(id);
    }

}
